package com.tsingteng.cosfun.utils;

import android.media.MediaPlayer;

import java.io.File;
import java.io.IOException;

/**
 * 描述 :音乐播放器utils
 * 创建人: 肖利.
 * 时间:2018/10/17
 * qq(872179966)
 */

public class MusicUtils {
    private static MusicUtils musicUtils = null;
    private MediaPlayer mediaPlayer;

    private MusicUtils() {

    }


    public static MusicUtils getMusicUtils() {
        if (musicUtils == null) {
            synchronized (MusicUtils.class) {
                if (musicUtils == null) {
                    musicUtils = new MusicUtils();
                }

            }
        }
        return musicUtils;
    }

    public MusicUtils getMediaPlayr() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
        return this;
    }


    public void setMediaPlayerUrl(String musicUrl) {
        if (mediaPlayer != null) {
            try {
                destoryMediaPlayer();
                getMediaPlayr();
                mediaPlayer.setDataSource(musicUrl);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public void playMediaPlayer() {
        if (mediaPlayer != null || mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

    public void pauseMediaPlayer() {
        if (mediaPlayer != null || mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    public void stopMediaPlayer() {
        if (mediaPlayer != null || mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            getMediaPlayr();
        }
    }

    public void destoryMediaPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }


}
